<?php

	class source
	{

		var $fw		= null;
		var $sec	= null;

		function source(&$framework, &$security)
		{

			$this->fw	= &$framework;
			$this->sec	= &$security;

			switch($this->fw->do)
			{

				case 'process':
					$this->process_invite();
					break;

				default:
					$this->show_invite();
					break;

			}

		}

		function process_invite()
		{

			$email_address	= trim($_POST['email_address']);

			// Time for error checking
			if(empty($email_address)) {
				$err['email_address']	= 'Field Required';
			} elseif(!$this->fw->is_email($email_address)) {
				$err['email_address']	= 'Invalid email address provided';
			}

			if(!isset($err)) {
				// Ensure that this email address doesnt already exist!
				$sql	= $this->fw->_pq("SELECT member_id FROM #__member WHERE member_email = '%s'", $email_address);
				$result	= mysql_query($sql) or die(mysql_error());

				if(mysql_num_rows($result) == 0) {
					// Checking finished. Lets send them an email.
					$member_id		= $this->sec->dtls['id'];
					$member_dtls	= $this->fw->get_member_details($member_id);

					if($member_dtls['is_referer'] === 1) {
						// --= Create new smarty object for template
						$smarty = new Smarty();
						$smarty->template_dir = _BASE_PATH .'/templates/'. $this->fw->config['smarty_template'] .'/';

						$smarty->assign('config', $this->fw->config);
						$smarty->assign('email_address', $email_address);
						$smarty->assign('member', $member_dtls);

						// Confirmation URL
						$code	= md5($member_dtls['id'] . $email_address . $member_dtls['email'] . $member_dtls['approve_date']);
						$url	= $this->fw->config['base_url'] . 'index.php?do=register&email='. $email_address .'&cc='. $code .'&ref='. $member_id;
						$smarty->assign('url', $url);

						$content	= $smarty->fetch('invite.email.text.tpl');

						$header = "From: Movie Manager (". $member_dtls['username'] ." <". $member_dtls['email'] .">\n";
						$header .= "To: $email_address <$email_address>\n";

						$m	= mail($email_address, 'Invitation to Movie Manager', $content, $header);

						unset($smarty);

						$_SESSION['msg'] = $email_address .' has been e-mailed with an invitation';
						header('location: index.php?what=invite');
						exit;

					} else {
						$this->show_invite('Your account does not have privlages to invite new members');
					}
				} else {
					$this->show_invite('Provided email address already exists');
				}
			} else {
				$this->show_invite('Errors Found', $err);
			}
		}

		function show_invite($err_msg = null, $err = array())
		{
			$this->fw->pc_item('Invite', 'Invite a new member to join the movie manager database');

			if($this->fw->do == 'process') {
				$email_address	= trim(htmlentities($_POST['email_address']));
			} else {
				$email_address	= null;
			}

			$this->fw->template	= 'invite.form.tpl';

			$this->fw->content['email_address']	= $email_address;

		}

	}

?>